home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / unarced / graphics / anim / anim.spec < prev    next >
Text File  |  1995-03-17  |  24KB  |  462 lines

  1. TITLE:  New ANIM spec (with typos corrected)
  2.  
  3.                               A N I M
  4.                   An IFF Format For CEL Animations
  5.  
  6.                     Revision date:  4 May 1988
  7.  
  8.                      prepared by:
  9.                           SPARTA Inc.
  10.                           23041 de la Carlota
  11.                           Laguna Hills, Calif 92653
  12.                           (714) 768-8161
  13.                           contact: Gary Bonham
  14.  
  15.                      also by:
  16.                           Aegis Development Co.
  17.                           2115 Pico Blvd.
  18.                           Santa Monica, Calif 90405
  19.                           213) 392-9972
  20.  
  21.  
  22. 1.0 Introduction
  23.    
  24.    The ANIM IFF format was developed at Sparta originally for the
  25.    production of animated video sequences on the Amiga computer.  The
  26.    intent was to be able to store, and play back, sequences of frames
  27.    and to minimize both the storage space on disk (through compression)
  28.    and playback time (through efficient de-compression algorithms).
  29.    It was desired to maintain maximum compatibility with existing
  30.    IFF formats and to be able to display the initial frame as a normal
  31.    still IFF picture.
  32.    
  33.    Several compression schemes have been introduced in the ANIM format.
  34.    Most of these are strictly of historical interest as the only one
  35.    currently being placed in new code is the vertical run length
  36.    encoded byte encoding developed by Jim Kent.
  37.    
  38.    1.1 ANIM Format Overview
  39.       
  40.       The general philosophy of ANIMs is to present the initial frame
  41.       as a normal, run-length-encoded, IFF picture.  Subsequent
  42.       frames are then described by listing only their differences
  43.       from a previous frame.  Normally, the "previous" frame is two
  44.       frames back as that is the frame remaining in the hidden 
  45.       screen buffer when double-buffering is used.  To better
  46.       understand this, suppose one has two screens, called A and B,
  47.       and the ability to instantly switch the display from one to
  48.       the other.  The normal playback mode is to load the initial
  49.       frame into A and duplicate it into B.  Then frame A is displayed
  50.       on the screen.  Then the differences for frame 2 are used to
  51.       alter screen B and it is displayed.  Then the differences for
  52.       frame 3 are used to alter screen A and it is displayed, and so
  53.       on.  Note that frame 2 is stored as differences from frame 1,
  54.       but all other frames are stored as differences from two frames
  55.       back.
  56.       
  57.       ANIM is an IFF FORM and its basic format is as follows (this
  58.       assumes the reader has a basic understanding of IFF format
  59.       files):
  60.                       FORM ANIM
  61.                       . FORM ILBM         first frame
  62.                       . . BMHD                normal type IFF data
  63.                       . . ANHD                optional animation header
  64.                                               chunk for timing of 1st frame.
  65.                       . . CMAP
  66.                       . . BODY
  67.                       . FORM ILBM         frame 2
  68.                       . . ANHD                animation header chunk
  69.                       . . DLTA                delta mode data
  70.                       . FORM ILBM         frame 3
  71.                       . . ANHD
  72.                       . . DLTA
  73.                            ...
  74.       
  75.       The initial FORM ILBM can contain all the normal ILBM chunks,
  76.       such as CRNG, etc.  The BODY will normally be a standard
  77.       run-length-encoded data chunk (but may be any other legal
  78.       compression mode as indicated by the BMHD).  If desired, an ANHD
  79.       chunk can appear here to provide timing data for the first
  80.       frame.  If it is here, the operation field should be =0.
  81.       
  82.       The subsequent FORMs ILBM contain an ANHD, instead of a BMHD,
  83.       which duplicates some of BMHD and has additional parameters
  84.       pertaining to the animation frame.  The DLTA chunk contains
  85.       the data for the delta compression modes.  If
  86.       the older XOR compression mode is used, then a BODY chunk
  87.       will be here.  In addition, other chunks may be placed in each
  88.       of these as deemed necessary (and as code is placed in player
  89.       programs to utilize them).  A good example would be CMAP chunks
  90.       to alter the color palette.  A basic assumption in ANIMs is
  91.       that the size of the bitmap, and the display mode (e.g. HAM)
  92.       will not change through the animation.  Take care when playing
  93.       an ANIM that if a CMAP occurs with a frame, then the change must
  94.       be applied to both buffers.
  95.       
  96.       Note that the DLTA chunks are not interleaved bitmap representations,
  97.       thus the use of the ILBM form is inappropriate for these frames.  
  98.       However, this inconsistency was not noted until there were a number
  99.       of commercial products either released or close to release which
  100.       generated/played this format.  Therefore, this is probably an
  101.       inconsistency which will have to stay with us.
  102.  
  103.    1.2 Recording ANIMs
  104.  
  105.       To record an ANIM will require three bitmaps - one for 
  106.       creation of the next frame, and two more for a "history" of the
  107.       previous two frames for performing the compression calculations
  108.       (e.g. the delta mode calculations).
  109.       
  110.       There are five frame-to-frame compression methods currently
  111.       defined.  The first three are mainly for historical interest.
  112.       The product Aegis VideoScape 3D utilizes the third method in
  113.       version 1.0, but switched to method 5 on 2.0.  This is
  114.       the only instance known of a commercial product generating
  115.       ANIMs of any of the first three methods.  The fourth method
  116.       is a general short or long word compression scheme which has
  117.       several options including whether the compression is horizontal
  118.       or vertical, and whether or not it is XOR format.  This offers
  119.       a choice to the user for the optimization of file size and/or
  120.       playback speed.  The fifth method is the byte vertical run length
  121.       encoding as designed by Jim Kent.  Do not confuse
  122.       this with Jim's RIFF file format which is different than ANIM.
  123.       Here we utilized his compression/decompression routines within the
  124.       ANIM file structure.
  125.       
  126.       The following paragraphs give a general outline of each of the
  127.       methods of compression currently included in this spec.
  128.  
  129.       1.2.1 XOR mode
  130.          
  131.          This mode is the original and is included here for historical
  132.          interest.  In general, the delta modes are far superior.
  133.          The creation of XOR mode is quite simple.  One simply
  134.          performs an exclusive-or (XOR) between all corresponding
  135.          bytes of the new frame and two frames back.  This results
  136.          in a new bitmap with 0 bits wherever the two frames were
  137.          identical, and 1 bits where they are different.  Then this
  138.          new bitmap is saved using run-length-encoding.  A major
  139.          obstacle of this mode is in the time consumed in performing
  140.          the XOR upon reconstructing the image.
  141.          
  142.       1.2.2 Long Delta mode
  143.          
  144.          This mode stores the actual new frame long-words which are
  145.          different, along with the offset in the bitmap.  The
  146.          exact format is shown and discussed in section 2 below.
  147.          Each plane is handled separately, with no data being saved
  148.          if no changes take place in a given plane.  Strings of
  149.          2 or more long-words in a row which change can be run
  150.          together so offsets do not have to be saved for each one.
  151.          
  152.          Constructing this data chunk usually consists of having
  153.          a buffer to hold the data, and calculating the data as
  154.          one compares the new frame, long-word by long-word, with
  155.          two frames back.
  156.          
  157.       1.2.3 Short Delta mode
  158.          
  159.          This mode is identical to the Long Delta mode except that
  160.          short-words are saved instead of long-words.  In most
  161.          instances, this mode results in a smaller DLTA chunk.
  162.          The Long Delta mode is mainly of interest in improving
  163.          the playback speed when used on a 32-bit 68020 Turbo Amiga.
  164.          
  165.       1.2.4 General Delta mode
  166.  
  167.          The above two delta compression modes were hastily put together.
  168.          This mode was an attempt to provide a well-thought-out delta
  169.          compression scheme.  Options provide for both short and long
  170.          word compression, either vertical or horizontal compression,
  171.          XOR mode (which permits reverse playback), etc.  About the time
  172.          this was being finalized, the fifth mode, below, was developed
  173.          by Jim Kent.  In practice the short-vertical-run-length-encoded
  174.          deltas in this mode play back faster than the fifth mode (which
  175.          is in essence a byte-vertical-run-length-encoded delta mode) but
  176.          does not compress as well - especially for very noisy data such
  177.          as digitized images.  In most cases, playback speed not being
  178.          terrifically slower, the better compression (sometimes 2x) is
  179.          preferable due to limited storage media in most machines.
  180.  
  181.          Details on this method are contained in section 2.2.2 below.
  182.  
  183.       1.2.5 Byte Vertical Compression
  184.  
  185.          This method does not offer the many options that method 4 offers,
  186.          but is very successful at producing decent compression even for
  187.          very noisy data such as digitized images.  The method was devised
  188.          by Jim Kent and is utilized in his RIFF file format which is 
  189.          different than the ANIM format.  The description of this method
  190.          in this document is taken from Jim's writings.  Further, he has
  191.          released both compression and decompression code to public domain.
  192.          
  193.          Details on this method are contained in section 2.2.3 below.
  194.  
  195.    1.3 Playing ANIMs
  196.       
  197.       Playback of ANIMs will usually require two buffers, as mentioned
  198.       above, and double-buffering between them.  The frame data from
  199.       the ANIM file is used to modify the hidden frame to the next
  200.       frame to be shown.  When using the XOR mode, the usual run-
  201.       length-decoding routine can be easily modified to do the 
  202.       exclusive-or operation required.  Note that runs of zero bytes,
  203.       which will be very common, can be ignored, as an exclusive or
  204.       of any byte value to a byte of zero will not alter the original
  205.       byte value.
  206.       
  207.       The general procedure, for all compression techniques, is to first
  208.       decode the initial ILBM picture into the hidden buffer and double-
  209.       buffer it into view.  Then this picture is copied to the other (now
  210.       hidden) buffer.  At this point each frame is displayed with the
  211.       same procedure.  The next frame is formed in the hidden buffer by
  212.       applying the DLTA data (or the XOR data from the BODY chunk in the
  213.       case of the first XOR method) and the new frame is double-buffered
  214.       into view.  This process continues to the end of the file.
  215.  
  216.       A master colormap should be kept for the entire ANIM which would
  217.       be initially set from the CMAP chunk in the initial ILBM.  This
  218.       colormap should be used for each frame.  If a CMAP chunk appears
  219.       in one of the frames, then this master colormap is updated and the
  220.       new colormap applies to all frames until the occurrance of another
  221.       CMAP chunk.
  222.  
  223.       Looping ANIMs may be constructed by simply making the last two frames
  224.       identical to the first two.  Since the first two frames are special
  225.       cases (the first being a normal ILBM and the second being a delta from
  226.       the first) one can continually loop the anim by repeating from frame
  227.       three.  In this case the delta for creating frame three will modify
  228.       the next to the last frame which is in the hidden buffer (which is
  229.       identical to the first frame), and the delta for creating frame four
  230.       will modify the last frame which is identical to the second frame.
  231.  
  232.       Multi-File ANIMs are also supported so long as the first two frames
  233.       of a subsequent file are identical to the last two frames of the
  234.       preceeding file.  Upon reading subsequent files, the ILBMs for the
  235.       first two frames are simply ignored, and the remaining frames are
  236.       simply appended to the preceeding frames.  This permits splitting
  237.       ANIMs across multiple floppies and also permits playing each section
  238.       independently and/or editing it independent of the rest of the ANIM.
  239.       
  240.       Timing of ANIM playback is easily achieved using the vertical blank
  241.       interrupt of the Amiga.  There is an example of setting up such
  242.       a timer in the ROM Kernel Manual.  Be sure to remember the timer
  243.       value when a frame is flipped up, so the next frame can be flipped
  244.       up relative to that time.  This will make the playback independent
  245.       of how long it takes to decompress a frame (so long as there is enough
  246.       time between frames to accomplish this decompression).
  247.  
  248. 2.0 Chunk Formats
  249.    2.1 ANHD Chunk
  250.       The ANHD chunk consists of the following data structure:
  251.       
  252.            UBYTE operation  The compression method:
  253.                             =0 set directly (normal ILBM BODY),
  254.                             =1 XOR ILBM mode,
  255.                             =2 Long Delta mode,
  256.                             =3 Short Delta mode,
  257.                             =4 Generalized short/long Delta mode,
  258.                             =5 Byte Vertical Delta mode
  259.                             =74 (ascii 'J') reserved for Eric Graham's
  260.                                compression technique (details to be
  261.                                released later).
  262.  
  263.            UBYTE mask      (XOR mode only - plane mask where each
  264.                             bit is set =1 if there is data and =0
  265.                             if not.)
  266.            UWORD w,h       (XOR mode only - width and height of the
  267.                             area represented by the BODY to eliminate
  268.                             unnecessary un-changed data)
  269.            WORD  x,y       (XOR mode only - position of rectangular
  270.                             area representd by the BODY)
  271.            ULONG abstime   (currently unused - timing for a frame
  272.                             relative to the time the first frame
  273.                             was displayed - in jiffies (1/60 sec))
  274.            ULONG reltime   (timing for frame relative to time
  275.                             previous frame was displayed - in
  276.                             jiffies (1/60 sec))
  277.            UBYTE interleave (unused so far - indicates how may frames
  278.                              back this data is to modify.  =0 defaults
  279.                              to indicate two frames back (for double
  280.                              buffering). =n indicates n frames back.
  281.                              The main intent here is to allow values
  282.                              of =1 for special applications where
  283.                              frame data would modify the immediately
  284.                              previous frame)
  285.            UBYTE pad0        Pad byte, not used at present.
  286.            ULONG bits        32 option bits used by options=4 and 5.
  287.                              At present only 6 are identified, but the
  288.                              rest are set =0 so they can be used to
  289.                              implement future ideas.  These are defined
  290.                              for option 4 only at this point.  It is
  291.                              recommended that all bits be set =0 for
  292.                              option 5 and that any bit settings
  293.                              used in the future (such as for XOR mode)
  294.                              be compatible with the option 4
  295.                              bit settings.   Player code should check
  296.                              undefined bits in options 4 and 5 to assure
  297.                              they are zero.
  298.  
  299.                              The six bits for current use are:
  300.  
  301.                              bit #              set =0               set =1
  302.                              ===============================================
  303.                              0              short data           long data
  304.                              1                 set                  XOR
  305.                              2             separate info        one info list
  306.                                            for each plane       for all planes
  307.                              3               not RLC        RLC (run length coded)
  308.                              4              horizontal           vertical
  309.                              5           short info offsets   long info offsets
  310.  
  311.            UBYTE pad[16]     This is a pad for future use for future
  312.                              compression modes.
  313.       
  314.    2.2 DLTA Chunk
  315.       
  316.       This chunk is the basic data chunk used to hold delta compression
  317.       data.  The format of the data will be dependent upon the exact
  318.       compression format selected.  At present there are two basic
  319.       formats for the overall structure of this chunk.
  320.  
  321.       2.2.1 Format for methods 2 & 3
  322.  
  323.          This chunk is a basic data chunk used to hold the delta
  324.          compression data.  The minimum size of this chunk is 32 bytes
  325.          as the first 8 long-words are byte pointers into the chunk for
  326.          the data for each of up to 8 bitplanes.  The pointer for the
  327.          plane data starting immediately following these 8 pointers will
  328.          have a value of 32 as the data starts in the 33-rd byte of the
  329.          chunk (index value of 32 due to zero-base indexing).
  330.       
  331.          The data for a given plane consists of groups of data words.  In
  332.          Long Delta mode, these groups consist of both short and long
  333.          words - short words for offsets and numbers, and long words for
  334.          the actual data.  In Short Delta mode, the groups are identical
  335.          except data words are also shorts so all data is short words.
  336.          Each group consists of a starting word which is an offset.  If
  337.          the offset is positive then it indicates the increment in long
  338.          or short words (whichever is appropriate) through the bitplane.
  339.          In other words, if you were reconstructing the plane, you would
  340.          start a pointer (to shorts or longs depending on the mode) to
  341.          point to the first word of the bitplane.  Then the offset would
  342.          be added to it and the following data word would be placed at
  343.          that position.  Then the next offset would be added to the
  344.          pointer and the following data word would be placed at that
  345.          position.  And so on...  The data terminates with an offset
  346.          equal to 0xFFFF.
  347.       
  348.          A second interpretation is given if the offset is negative.  In
  349.          that case, the absolute value is the offset+2.  Then the 
  350.          following short-word indicates the number of data words that
  351.          follow.  Following that is the indicated number of contiguous
  352.          data words (longs or shorts depending on mode) which are to
  353.          be placed in contiguous locations of the bitplane.
  354.       
  355.          If there are no changed words in a given plane, then the pointer
  356.          in the first 32 bytes of the chunk is =0.
  357.       
  358.       2.2.2 Format for method 4
  359.          
  360.          The DLTA chunk is modified slightly to have 16 long pointers at
  361.          the start.  The first 8 are as before - pointers to the start of
  362.          the data for each of the bitplanes (up to a theoretical max of 8
  363.          planes).  The next 8 are pointers to the start of the offset/numbers
  364.          data list.  If there is only one list of offset/numbers for all
  365.          planes, then the pointer to that list is repeated in all positions
  366.          so the playback code need not even be aware of it.  In fact, one
  367.          could get fancy and have some bitplanes share lists while others
  368.          have different lists, or no lists (the problems in these schemes
  369.          lie in the generation, not in the playback).
  370.  
  371.          The best way to show the use of this format is in a sample playback
  372.          routine.
  373.  
  374.             SetDLTAshort(bm,deltaword)
  375.             struct BitMap *bm;
  376.             WORD *deltaword;
  377.             {
  378.                int i;
  379.                LONG *deltadata;
  380.                WORD *ptr,*planeptr;
  381.                register int s,size,nw;
  382.                register WORD *data,*dest;
  383.  
  384.                deltadata = (LONG *)deltaword;
  385.                nw = bm->BytesPerRow >>1;
  386.  
  387.                for (i=0;i<bm->Depth;i++) {
  388.                   planeptr = (WORD *)(bm->Planes[i]);
  389.                   data = deltaword + deltadata[i];
  390.                   ptr  = deltaword + deltadata[i+8];
  391.                   while (*ptr != 0xFFFF) {
  392.                      dest = planeptr + *ptr++;
  393.                      size = *ptr++;
  394.                      if (size < 0) {
  395.                         for (s=size;s<0;s++) {
  396.                            *dest = *data;
  397.                            dest += nw;
  398.                         }
  399.                         data++;
  400.                      }
  401.                      else {
  402.                         for (s=0;s<size;s++) {
  403.                            *dest = *data++;
  404.                            dest += nw;
  405.                         }
  406.                      }
  407.                   }
  408.                }
  409.                return(0);
  410.             }
  411.  
  412.          The above routine is for short word vertical compression with
  413.          run length compression.  The most efficient way to support 
  414.          the various options is to replicate this routine and make 
  415.          alterations for, say, long word or XOR.  The variable nw
  416.          indicates the number of words to skip to go down the vertical
  417.          column.  This one routine could easily handle horizontal
  418.          compression by simply setting nw=1.  For ultimate playback
  419.          speed, the core, at least, of this routine should be coded in
  420.          assembly language.
  421.  
  422.       2.2.2 Format for method 5
  423.  
  424.          In this method the same 16 pointers are used as in option 4.
  425.          The first 8 are pointers to the data for up to 8 planes.
  426.          The second set of 8 are not used but were retained for several
  427.          reasons.  First to be somewhat compatible with code for option
  428.          4 (although this has not proven to be of any benefit) and 
  429.          second, to allow extending the format for more bitplanes (code
  430.          has been written for up to 12 planes).  
  431.  
  432.          Compression/decompression is performed on a plane-by-plane basis.
  433.          For each plane, compression can be handled by the skip.c code
  434.          (provided Public Domain by Jim Kent) and decompression can be
  435.          handled by unvscomp.asm (also provided Public Domain by Jim Kent).
  436.          
  437.          Compression/decompression is performed on a plane-by-plane basis.
  438.          The following description of the method is taken directly from
  439.          Jim Kent's code with minor re-wording.  Please refer to Jim's
  440.          code (skip.c and unvscomp.asm) for more details:
  441.  
  442.             Each column of the bitplane is compressed separately.
  443.             A 320x200 bitplane would have 40 columns of 200 bytes each.
  444.             Each column starts with an op-count followed by a number
  445.             of ops.  If the op-count is zero, that's ok, it just means
  446.             there's no change in this column from the last frame.
  447.             The ops are of three classes, and followed by a varying
  448.             amount of data depending on which class:
  449.               1. Skip ops - this is a byte with the hi bit clear that
  450.                  says how many rows to move the "dest" pointer forward,
  451.                  ie to skip. It is non-zero.
  452.               2. Uniq ops - this is a byte with the hi bit set.  The hi
  453.                  bit is masked down and the remainder is a count of the
  454.                  number of bytes of data to copy literally.  It's of
  455.                  course followed by the data to copy.
  456.               3. Same ops - this is a 0 byte followed by a count byte,
  457.                  followed by a byte value to repeat count times.
  458.             Do bear in mind that the data is compressed vertically rather
  459.             than horizontally, so to get to the next byte in the destination
  460.             we add the number of bytes per row instead of one!
  461.  
  462.